package cn.mothin.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import cn.mothin.entity.RoleModulePermission;
import cn.mothin.frame.core.base.BaseDao;
import cn.mothin.frame.help.UniqueCode;

@Repository("cc8dc445-4c7b-4533-9137-2d599416b88f")
public class RoleModulePermissionDao extends BaseDao<RoleModulePermission> {
	public List<RoleModulePermission> findByRole(String roleId) {
		String sql = "SELECT * FROM role_module_permission WHERE targetId = ?";
		return query(sql, roleId);
	}

	public boolean checkPermission(String moduleItemId, String roleId) {
		String sql = "SELECT COUNT(id) FROM role_module_permission WHERE moduleItemId = ? AND targetId = ?";
		return sqlHelper.queryCount(sql, moduleItemId, roleId) > 0;
	}

	public String addRolePermission(String targetId, String moduleItemId) {
		String id = UniqueCode.make();
		RoleModulePermission roleModulePermission = new RoleModulePermission();
		roleModulePermission.setId(id);
		roleModulePermission.setModuleItemId(moduleItemId);
		roleModulePermission.setTargetId(targetId);
		save(roleModulePermission);
		return id;
	}
}
